package org.ace.biz.user.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import org.ace.biz.user.ITeamBiz;
import org.ace.model.TeamModel;
import org.springframework.stereotype.Service;
@Service("teamBiz")
public class TeamBiz implements ITeamBiz {

	/*队伍ID 和 队伍 数据模型的 映射*/
	private Map<Integer,TeamModel> teams= new ConcurrentHashMap<Integer, TeamModel>();
	/*用户ID 与所在队伍ID 映射*/
	private Map<Integer,Integer> userTeam = new ConcurrentHashMap<Integer, Integer>();
	
	private AtomicInteger key =new AtomicInteger(0);
	
	/*重复利用的队伍列表*/
	private List<TeamModel>  last = new CopyOnWriteArrayList<TeamModel>();
	
	@Override
	public TeamModel get(Integer teamId) {
		return teams.get(teamId);
	}

	@Override
	public TeamModel getByUserId(Integer userId) {
		
		return get(getTeamId(userId));
	}

	@Override
	public boolean hasTeam(Integer userId) {
		
		return teams.containsKey(userId);
	}

	@Override
	public Integer getTeamId(Integer userId) {
		
		return userTeam.get(userId);
	}

	@Override
	public Integer kick(Integer userId, Integer targetId) {
		/*没有队伍 不能踢人*/
		if(!userTeam.containsKey(userId)) return null;
		/*获取主动踢人所在队伍数据模型*/
		TeamModel team = get(userTeam.get(userId));
		/*不是队长的话不能踢人*/
		if(!team.getLeaderId().equals(userId)) return null;
		/* 两个 集合 中信息不匹配 也无法 踢人*/
		if(!userTeam.get(userId).equals(team.getId())) return null;
		
		team.getPlayers().remove(targetId);
		userTeam.remove(targetId);
		return team.getId();
	}

	@Override
	public List<Integer> dismiss(Integer userId) {
		/*没有队伍无法解散*/
		if(!userTeam.containsKey(userId)) return null;
        /*获取teams 集合中映射的team模型*/
		TeamModel team=teams.get(userTeam.get(userId));
		/*不是队长 无权解散队伍*/
		if(!team.getLeaderId().equals(userId)) return null;
		
		/*存储玩家id列表*/
		List<Integer> playerIdList = new ArrayList<Integer>();
		for(Integer playerId : team.getPlayers())
		{//移除已解散的队伍
			userTeam.remove(playerId);
			playerIdList.add(playerId);
		}
		/*重复利用 队伍模型*/
		last.add(team);
		//返回已解散的队伍的成员id
		return playerIdList;
	}

	@Override
	public boolean leave(Integer userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public TeamModel join(Integer userId, Integer teamId) {
		TeamModel team = get(teamId);
		/*队伍不存在 无法加入*/
		if(team==null) return null;
		/*队伍已满 无法加入*/
		if(team.getPlayers().size()>=4) return null;
		//加入队伍
		team.getPlayers().add(userId);
		//添加到 玩家 队伍 id 映射
		userTeam.put(userId, teamId);
		return team;
	}
	
	@Override
	public TeamModel create(Integer userId) {
		// 创建队伍
		TeamModel team;
		if(last.size()>0)
		{
			team=last.get(0);
			last.remove(0);
		}else
		{
			team = new TeamModel();
			team.setId(key.getAndIncrement());
		}
		//设置Team的参数
		team.setLeaderId(userId);
		List<Integer> players=new CopyOnWriteArrayList<Integer>();
		players.add(userId);
		team.setPlayers(players);
		teams.put(team.getId(), team);
		userTeam.put(userId, team.getId());
		return team;
	}

	@Override
	public void userTeamRemove(Integer userId) {
		userTeam.remove(userId);

	}

	@Override
	public void addLast(TeamModel team) {
		last.add(team);
	}

}
